package com;

import javax.swing.*;
import com.trusfort.redis.DBIndex;
import com.trusfort.redis.JedisClient;
import com.trusfort.redis.RedisConnectionPool;
import com.trusfort.redis.Smember;
import com.util.FileUtils;
import com.util.Log;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.ScanResult;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.awt.event.ActionEvent;

public class RedisAddInvalidData2 {
	private static JTextField txtHost;
	private static JTextField txtPort;
	private static long total = 0;

	public static String[] redisKeys = { "XDID::aimei", "XDID::abmac", "XDID::amac", "XDID::aaid", "XDID::abid",
			"XDID::absn", "XDID::afpid", "XDID::afdid", "XDID::asn", "XDID::acid" };
	private static JTextArea txtLog;
	private static JTextField txtLimit;

	public static void logInfo(String msg) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String cur = sdf.format(new Date());

		System.out.println(msg);
		txtLog.append(msg + "\n");
	}

	/**
	 * 判断是否为数字格式
	 * 
	 * @param s
	 * @return
	 */
	public final static boolean isNumeric(String s) {
		if (s != null && !"".equals(s.trim()))
			return s.matches("^[0-9]*$");
		else
			return false;

	}

	public void addInvalidRedisData(JedisClient client, int limitValue) throws Exception {

		int seconds = 60 * 60 * 24 * 180;
		for (int j = 0; j < limitValue; j++) {
			
			logInfo("正在插入第"+(j+1)+"个设备特征缓存数据");
			
			java.util.List<Smember> smemberList = new ArrayList<>();
			java.util.List<Map<String, String>> hmsetList = new ArrayList<Map<String,String>>();
			
			
			for (int i = 0; i < 500; i++) {
				
				Map<String, String> po = new HashMap<String, String>();
				String devid = UUID.randomUUID().toString().replace("-", "").substring(0, 32);

				po.put("devid", devid);
				po.put("devfp", "");
				po.put("model", "");
				po.put("brand", "");

				String imei = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String btMac = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String mac = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String androidId = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String bootId = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String bootSn = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String fpId = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String udid = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String serial = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String installTime = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String cid = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
				String udidRand = UUID.randomUUID().toString().replace("-", "").substring(0, 10);

				po.put("f0_android_id", androidId);
				po.put("f1_boot_sn", bootSn);
				po.put("f2_boot_id", bootId);
				po.put("f3_btmac", btMac);
				po.put("f4_cid", cid);
				po.put("f5_fpid", fpId);
				po.put("f6_imei", imei);
				po.put("f7_mac", mac);
				po.put("f9_udid", udid);
				po.put("f8_serial", serial);
				po.put("f10_udid_rand", udidRand);
				po.put("f11_install_time", installTime);

				smemberList.add(new Smember("XDID::aaid" + androidId, devid));
				smemberList.add(new Smember("XDID::absn" + bootSn, devid));
				smemberList.add(new Smember("XDID::abid" + bootId, devid));
				smemberList.add(new Smember("XDID::abmac" + btMac, devid));
				smemberList.add(new Smember("XDID::acid" + cid, devid));
				smemberList.add(new Smember("XDID::afpid" + fpId, fpId));
				smemberList.add(new Smember("XDID::aimei" + imei, devid));
				smemberList.add(new Smember("XDID::amac" + mac, devid));
				smemberList.add(new Smember("XDID::afdid" + udid, devid));
				smemberList.add(new Smember("XDID::asn" + serial, devid));
				smemberList.add(new Smember("XDID::afdidrand" + udidRand, devid));
				smemberList.add(new Smember("XDID::afit" + installTime, devid));

				hmsetList.add(po);
			}

			client.sadd(DBIndex.XDID_DB_INDEX, smemberList, seconds);
			client.hmset(DBIndex.XDID_DB_INDEX, hmsetList, seconds);
			smemberList.clear();
			hmsetList.clear();
		}
	}

	public void statFieldCount() {

		try {

			String ip = txtHost.getText();
			int port = Integer.valueOf(txtPort.getText());
			JedisClient client = new JedisClient(ip, port, null);

			logInfo("插入无效Redis特征数据开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

			String limit = txtLimit.getText();

			if (!isNumeric(limit)) {
				logInfo("条数请输入数字格式");
				return;
			}

			int limitValue = Integer.parseInt(limit);

			addInvalidRedisData(client,limitValue);
			logInfo("插入无效Redis特征数据结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

		} catch (Exception e1) {
			logInfo("Redis异常>>" + e1.fillInStackTrace());
		}
	}

	public static void main(String[] args) {
		JFrame jf = new JFrame("Redis数据分析");
		jf.setTitle("数据插入");
		jf.setSize(839, 481);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);

		// 创建内容面板，指定使用 流式布局
		JPanel panel = new JPanel();
		panel.setLayout(null);
		JButton btn05 = new JButton("添加");
		btn05.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				new Thread(new Runnable() {// 启动数据获取，更新显示进程

					public void run() {
						RedisAddInvalidData2 main = new RedisAddInvalidData2();
						// 获取每种特征key个数
						main.statFieldCount();
					}
				}).start(); //
			}
		});
		btn05.setBounds(704, 33, 69, 23);
		panel.add(btn05);

		jf.setContentPane(panel);

		txtHost = new JTextField();
		txtHost.setText("192.168.1.170");
		txtHost.setBounds(101, 33, 163, 21);
		panel.add(txtHost);
		txtHost.setColumns(10);

		JLabel lblIp = new JLabel("IP:");
		lblIp.setBounds(68, 36, 54, 15);
		panel.add(lblIp);

		JLabel label = new JLabel("端口：");
		label.setBounds(274, 37, 54, 15);
		panel.add(label);

		txtPort = new JTextField();
		txtPort.setText("6386");
		txtPort.setColumns(10);
		txtPort.setBounds(328, 34, 163, 21);
		panel.add(txtPort);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(86, 112, 700, 286);
		panel.add(scrollPane);

		txtLog = new JTextArea();
		txtLog.setLineWrap(true);
		scrollPane.setViewportView(txtLog);
		txtLog.setColumns(10);

		JLabel label_2 = new JLabel("设备数：");
		label_2.setBounds(49, 69, 54, 15);
		panel.add(label_2);

		txtLimit = new JTextField();
		txtLimit.setText("10");
		txtLimit.setColumns(10);
		txtLimit.setBounds(101, 64, 119, 21);
		panel.add(txtLimit);
		jf.setVisible(true); // PS: 最后再设置为可显示(绘制), 所有添加的组件才会显示
	}
}